package com.ews.onlineexchange.util;

import com.ews.onlineexchange.model.CodeDic;

import java.util.List;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.Query;

@Named
@ApplicationScoped
public class CodeDicFinder {
	
	@Inject
	EntityManager entityManager;
		
	private static final String QUERY_EQL = ""
			+ "	SELECT			dic"
			+ "	FROM			CodeDic dic "
			+ "	WHERE			dic.codeType = :codeType	"
			+ "	ORDER BY 		dic.sort desc ,dic.codeValue											"	;
	public List<CodeDic> query(String codeType) {
		Query query = entityManager.createQuery(QUERY_EQL);
		query.setParameter("codeType", codeType);
		return query.getResultList();
	}
	
	private static final String QUERY_CODELIST_BY_TYPE_AND_VALUE_EQL = ""
			+ "	SELECT			dic"
			+ "	FROM			CodeDic dic "
			+ "	WHERE			dic.codeType = :codeType	"
			+ "	AND				dic.codeValue like 	:codeVlaue		"
			+ "	ORDER BY 		dic.sort desc ,dic.codeValue											"	;
	public List<CodeDic> queryCodeListByTypeAndValue(String codeType,String codeValue) {
		Query query = entityManager.createQuery(QUERY_CODELIST_BY_TYPE_AND_VALUE_EQL);
		query.setParameter("codeType", codeType);
		query.setParameter("codeVlaue", codeValue+"%");
		return query.getResultList();
	}
	
}
